﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WHControlLib.CommonHelpers;

namespace WHControlLib.Forms
{
    public partial class WHDialogFrm :WHForm
    {
        public WHDialogFrm()
        {
            InitializeComponent();
        }

        #region 属性定义
        private bool _isUseRadiusForm = true;
        [Category("A我的页面"), Description("是否启用圆角矩形窗口，默认，true "), Browsable(true)]
        public bool IsUseRadiusForm
        {
            get { return _isUseRadiusForm; }
            set { _isUseRadiusForm = value; Invalidate(); }
        }

        private float _radius = 8.0f;
        [Category("A我的页面"), Description("圆角矩形窗体的四角圆弧的程度，默认，8.0f，数字越大圆弧越小不能小于1.0f"), Browsable(true)]
        public float Radius
        {
            get { return _radius; }
            set
            {
                if (value < 1.0f)
                {
                    _radius = 1.0f;
                }
                _radius = value; Invalidate();
            }

        }
        private bool _isUseAmimateWindow;
        [Category("A我的页面"), Description("是否启用窗口动画，默认，否"), Browsable(true)]
        public bool IsUseAmimateWindow
        {
            get { return _isUseAmimateWindow; }
            set { _isUseAmimateWindow = value; }
        }

        public enum AmimateDEC
        {
            NearToFar, TopTOBouttom, BottomTOTop, LeftBottomToTop, RightBottomToTop,
            LeftToRight, RighToLeft


        }
        private AmimateDEC _myAminateDec = AmimateDEC.NearToFar;
        [Category("A我的页面"), Description("是否启用窗口动画后的动画方向，默认，否"), Browsable(true)]
        public AmimateDEC MyAminateDec
        {
            get { return _myAminateDec; }
            set { _myAminateDec = value; }
        }

        private int _aminateTime = 150;
        [Category("A我的页面"), Description("动画持续时间，默认，150ms ,不能小于10"), Browsable(true)]
        public int AminateTime
        {
            get { return _aminateTime; }
            set
            {
                if (value < 10)
                {
                    _aminateTime = 10;
                }
                else
                    _aminateTime = value;
            }
        }
        private bool _wHPageIsShowMaxMinBox;
        [DefaultValue(false)]
        public override bool IsShowMaxMinBox

        {
            get { return _wHPageIsShowMaxMinBox; }
            set { _wHPageIsShowMaxMinBox = value; Invalidate(); }

        }
        private bool _wHPageIsFillTitleBox;
        [DefaultValue(false)]
        public override bool IsFillTitleBox
        {
            get => _wHPageIsFillTitleBox;
            set { _wHPageIsFillTitleBox = value; Invalidate(); }
        }
        private bool _wHPageIsDrawIcon;
        [DefaultValue(false)]
        public override bool IsDrawIcon
        {
            get => _wHPageIsDrawIcon;
            set { _wHPageIsDrawIcon = value; Invalidate(); }
        }
        private int _wHPageCloseBoxOffsetX = 25;

        public override int CloseBoxOffsetX

        {
            get => _wHPageCloseBoxOffsetX;

            set
            {
                _wHPageCloseBoxOffsetX = value;
                Invalidate();
            }
        }

        #endregion
        //***********重写的方法*******************************
        public override void BeforePaintIni()
        {
            MyRect = this.ClientRectangle;

            DrawRct.X = MyRect.X + (int)FormBorderWidth / 2;
            DrawRct.Y = MyRect.Y + (int)FormBorderWidth / 2;
            DrawRct.Width = MyRect.Width - FormBorderWidth;
            DrawRct.Height = MyRect.Height - FormBorderWidth;
        }
        public override void DrawShowBody(Graphics Myg, Rectangle Myrect)
        {

            if (MyRect.Width <= 0 || MyRect.Height <= 0)
            {
                return;
            }
            if (IsUseRadiusForm)
            {
                using (SolidBrush backBrush = new SolidBrush(Color.Transparent))
                {
                    Myg.FillRectangle(backBrush, Myrect);

                }

                GraphicsPath RoundPath = new GraphicsPath();

                RoundPath = DrawHelper.GetRoundRectangePath(DrawRct, FormBorderWidth, Radius);


                if (IsUseTwoBackColor)
                {

                    Rectangle BodyBackRect = new Rectangle();
                    BodyBackRect.X = DrawRct.X;
                    BodyBackRect.Y = DrawRct.Y + TitleRect.Height + 2;
                    BodyBackRect.Width = DrawRct.Width;
                    BodyBackRect.Height = DrawRct.Height - TitleRect.Height + 2;

                    using (LinearGradientBrush BodyFillBrush = new LinearGradientBrush(BodyBackRect, FirstBackColor, SecondBackColor, LinearGradientMode.Vertical))
                    {
                        //Myg.FillRectangle(BodyFillBrush, BodyBackRect);
                        Myg.FillPath(BodyFillBrush, RoundPath);
                    }

                }
                else
                {
                    using (SolidBrush BodyFillBrush = new SolidBrush(FirstBackColor))
                    {
                        //Myg.FillRectangle(BodyFillBrush, MyRect);
                        Myg.FillPath(BodyFillBrush, RoundPath);
                    }
                }


            }
            //不使用圆角
            else
            {
                if (IsUseTwoBackColor)
                {

                    Rectangle BodyBackRect = new Rectangle();
                    BodyBackRect.X = MyRect.X;
                    BodyBackRect.Y = MyRect.Y + TitleRect.Height + 2;
                    BodyBackRect.Width = MyRect.Width;
                    BodyBackRect.Height = MyRect.Height - TitleRect.Height + 2;
                    using (LinearGradientBrush BodyFillBrush = new LinearGradientBrush(BodyBackRect, FirstBackColor, SecondBackColor, LinearGradientMode.Vertical))
                    {
                        Myg.FillRectangle(BodyFillBrush, BodyBackRect);
                    }

                }
                else
                {
                    using (SolidBrush BodyFillBrush = new SolidBrush(FirstBackColor))
                    {
                        Myg.FillRectangle(BodyFillBrush, MyRect);
                    }
                }

            }



        }

        public override void DrawBodyBorder(Graphics Myg, Rectangle MyRect)
        {
            //base.DrawBodyBorder(Myg, MyRect);
            if (MyRect.Width <= 0 || MyRect.Height <= 0)
            {
                return;
            }


            if (IsUseRadiusForm)
            {
                if (IsDrawFormBorder)
                {
                    using (Pen formBodyPen = new Pen(FormBorderColor, FormBorderWidth))
                    {
                        GraphicsPath formbodypath = new GraphicsPath();
                        formbodypath = DrawHelper.GetRoundRectangePath(DrawRct, FormBorderWidth, Radius);
                        Myg.DrawPath(formBodyPen, formbodypath);

                    }
                }

                //GraphicsPath formPath = new GraphicsPath();
                //formPath = DrawHelper.GetRoundRectangePath(MyRect, FormBorderWidth, Radius);
                //Region Formreg = new Region(formPath);
                //this.Region = Formreg;
            }
            //不使用圆角
            else
            {
                if (IsDrawFormBorder)
                {
                    int penWidht = FormBorderWidth / 2;
                    Rectangle borderRect = new Rectangle();
                    borderRect.X = penWidht;
                    borderRect.Y = penWidht;
                    borderRect.Width = MyRect.Width - FormBorderWidth;
                    borderRect.Height = MyRect.Height - FormBorderWidth;


                    using (SolidBrush BodyBorderBrush = new SolidBrush(FormBorderColor))
                    {
                        using (Pen BodyBorderPen = new Pen(BodyBorderBrush, FormBorderWidth))
                        {
                            Myg.DrawRectangle(BodyBorderPen, borderRect);
                        }
                    }
                }
            }

           

        }



        #region 窗体动画效果
        /// <summary>
        /// 窗体动画函数
        /// </summary>
        /// <param name="hwnd">指定产生动画的窗口的句柄</param>
        /// <param name="dwTime">指定动画持续的时间</param>
        /// <param name="dwFlags">指定动画类型，可以是一个或多个标志的组合。</param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "AnimateWindow")]
        private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);

        //下面是可用的常量，根据不同的动画效果声明自己需要的
        private const int AW_HOR_POSITIVE = 0x0001;//自左向右显示窗口，该标志可以在滚动动画和滑动动画中使用。使用AW_CENTER标志时忽略该标志
        private const int AW_HOR_NEGATIVE = 0x0002;//自右向左显示窗口，该标志可以在滚动动画和滑动动画中使用。使用AW_CENTER标志时忽略该标志
        private const int AW_VER_POSITIVE = 0x0004;//自顶向下显示窗口，该标志可以在滚动动画和滑动动画中使用。使用AW_CENTER标志时忽略该标志
        private const int AW_VER_NEGATIVE = 0x0008;//自下向上显示窗口，该标志可以在滚动动画和滑动动画中使用。使用AW_CENTER标志时忽略该标志该标志
        private const int AW_CENTER = 0x0010;//若使用了AW_HIDE标志，则使窗口向内重叠；否则向外扩展
        private const int AW_HIDE = 0x10000;//隐藏窗口
        private const int AW_ACTIVE = 0x20000;//**窗口，在使用了AW_HIDE标志后不要使用这个标志
        private const int AW_SLIDE = 0x40000;//使用滑动类型动画效果，默认为滚动动画类型，当使用AW_CENTER标志时，这个标志就被忽略
        private const int AW_BLEND = 0x80000;//使用淡入淡出效果
                                             //窗体代码（将窗体的FormBorderStyle属性设置为none）：

        private void WHPage_Load(object sender, EventArgs e)
        {
           

        }

        private void WHPage_FormClosing(object sender, FormClosingEventArgs e)
        {

            if (IsUseAmimateWindow)
            {
                AnimateWindow(this.Handle, AminateTime, AW_BLEND | AW_HIDE);
            }



        }

        private void WHDialogFrm_Resize(object sender, EventArgs e)
        {
            SetMyRegion();

        }

        private void WHDialogFrm_SizeChanged(object sender, EventArgs e)
        {
            
        }

        private void SetMyRegion()
        {
            Region Formreg;

            if (IsUseRadiusForm)
            {
                Rectangle myrect = this.ClientRectangle;

                GraphicsPath formPath = new GraphicsPath();
                formPath = DrawHelper.GetRoundRectangePath(myrect, FormBorderWidth, Radius);
                Formreg = new Region(formPath);
                this.Region = Formreg;
            }
            else
            {
                Rectangle myrect = this.ClientRectangle;

                GraphicsPath formPath = new GraphicsPath();
                //formPath = DrawHelper.GetRectangePath(myrect, FormBorderWidth, Radius);
                //     DrawHelper.GetRectangePath


                Formreg = new Region(myrect);
                this.Region = Formreg;
            }
        }

        private void WHDialogFrm_Load(object sender, EventArgs e)
        {
            if (IsUseAmimateWindow)
            {


                try
                {
               


                    int amimatedec = AW_CENTER | AW_ACTIVE;
                    switch (MyAminateDec)
                    {
                        case AmimateDEC.NearToFar:
                            amimatedec = AW_CENTER | AW_ACTIVE;
                            break;
                        case AmimateDEC.TopTOBouttom:
                            amimatedec = AW_VER_POSITIVE | AW_ACTIVE;
                            break;
                        case AmimateDEC.BottomTOTop:
                            amimatedec = AW_VER_NEGATIVE | AW_ACTIVE;
                            break;
                        case AmimateDEC.LeftBottomToTop:
                            amimatedec = AW_SLIDE | AW_VER_NEGATIVE | AW_ACTIVE;
                            break;
                        case AmimateDEC.RightBottomToTop:
                            amimatedec = AW_SLIDE | AW_VER_POSITIVE | AW_ACTIVE;
                            break;
                        case AmimateDEC.LeftToRight:
                            amimatedec = AW_HOR_POSITIVE | AW_ACTIVE;
                            break;
                        case AmimateDEC.RighToLeft:
                            amimatedec = AW_HOR_NEGATIVE | AW_ACTIVE;
                            break;
                        default:
                            break;
                    }
                    AnimateWindow(this.Handle, AminateTime, amimatedec);
                }
                catch (Exception)
                {


                }
            }
        }

        private void WHDialogFrm_Paint(object sender, PaintEventArgs e)
        {
           
        }
   

        #endregion





        ////////////////////////////////////////////////////////////






    }
}
